from . import models
from django.core.exceptions import ObjectDoesNotExist
from django.core import serializers


def searchinput_autofill(search_txt):
    """
    查找股票名称列表，并返回，便于web自动填充
    :param search_txt: 要查找的股票关键字
    :return: 返回股票名称列表和代码列表
    """
    if (search_txt.isdigit()):
        searchbyname = models.stock_basic.objects.filter(symbol__contains=search_txt)  # 如果输入的是数字，按股票代码查找
    else:
        searchbyname = models.stock_basic.objects.filter(name__contains=search_txt)  # 否则按股票名称查找
    namelist = []
    tscodelist = []
    for name in searchbyname:
        na = name.name
        namelist.append(na)
        tscode = name.ts_code
        tscodelist.append(tscode)
    return namelist, tscodelist


def query_ts_code(stockname):
    """
    按股票名称查找股票代码，返回股票代码
    :param stockname:
    :return:
    """
    try:
        ts_code = models.stock_basic.objects.get(name=stockname).ts_code
        return ts_code
    except ObjectDoesNotExist:
        ts_code = ''
        return (ts_code)


def query_daily_by_stockname(stockname):
    """
    获取指定股票名称的交易数据
    :param stockname:
    :return:
    """
    #如果输入的是代码
    if (len(stockname)==9): ts_code = stockname
    else:#如果输入的是股票名称
        ts_code = query_ts_code(stockname)
    if (ts_code == ''):
        return None
    q_daily = models.daily.objects.filter(ts_code=ts_code).order_by('-trade_date')[0:20]  # 最多返回20条记录
    return serializers.serialize("json", q_daily)


def query_daily_basic_by_stockname(stockname):
    """
    # 获取指定股票名称的交易指标数据
    :param stockname:
    :return:
    """
    # 如果输入的是代码
    if (len(stockname) == 9):
        ts_code = stockname
    else:  # 如果输入的是股票名称
        ts_code = query_ts_code(stockname)
    if (ts_code == ''):
        return None
    q_daily = models.daily_basic.objects.filter(ts_code=ts_code).order_by('-trade_date')[0:20]

    return serializers.serialize("json", q_daily)


def query_block_trade_by_stockname(stockname):
    """
    # 获取指定股票名称的大宗交易数据
    :param stockname:
    :return:
    """
    # 如果输入的是代码
    if (len(stockname) == 9):
        ts_code = stockname
    else:  # 如果输入的是股票名称
        ts_code = query_ts_code(stockname)
    if (ts_code == ''):
        return None
    q_daily = models.block_trade.objects.filter(ts_code=ts_code).order_by('-trade_date')[0:20]
    return serializers.serialize("json", q_daily)


def query_moneyflow_by_stockname(stockname):
    """
    # 获取指定股票名称的现金流交易数据
    :param stockname:
    :return:
    """
    # 如果输入的是代码
    if (len(stockname) == 9):
        ts_code = stockname
    else:  # 如果输入的是股票名称
        ts_code = query_ts_code(stockname)
    if (ts_code == ''):
        return None
    q_daily = models.moneyflow.objects.filter(ts_code=ts_code).order_by('-trade_date')[0:20]
    return serializers.serialize("json", q_daily)


def query_table_columns_chsname(tablename):
    """
    根据表名，查找表的字段中文含义，从table_info表中查找，table_info表内容在初始化时载入
    :param tablename:
    :return:
    """
    daily_table_columns = models.table_info.objects.filter(tablename=tablename)
    return serializers.serialize("json", daily_table_columns)
